package com.tss.controller;

import java.util.UUID;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tss.mapper.QuestionMapper;
import com.tss.pojo.Optionen;
import com.tss.pojo.User;
import com.tss.service.OptionenService;
import com.tss.service.RecordService;


@Controller
@RequestMapping("/test")
public class Test {
	
	@Autowired  
	private HttpSession session;
	
//	@Autowired
//	private HttpRequest request;
	
	@RequestMapping("/chat")
	public String chat(){
		return "chat";
	}
	
	@RequestMapping("/addcookie")
	public String addcookie(HttpServletRequest request,HttpServletResponse response,String token){
		
		
		Cookie cookie = new Cookie("token", token);
		cookie.setMaxAge(5);
		response.addCookie(cookie);
		return "hi";
	}
	
	@RequestMapping("/getcookie")
	public String cookieTest(HttpServletRequest request,HttpServletResponse response){
		Cookie[] cookies = request.getCookies();
		if(cookies==null){
			System.out.println("没有cookie");
			
		}else{
			for(Cookie cookie:cookies){
				if(cookie.getName().equals("token")){
//					cookie.setValue("0001");
//					cookie.setMaxAge(10*60);
//					response.addCookie(cookie);
					System.out.println(cookie.getValue());
					return "hi";
				}
			}
			System.out.println("write");
			Cookie cookie = new Cookie("token", "00002");
			response.addCookie(cookie);
		}
		
		
		return "hi";
	}
	
	@RequestMapping("/hi")
	public String sayHi(){
		System.out.println(session.getAttribute("i"));
		return "hi";
	}
	
	@RequestMapping("/add")
	public String add(){
		if(session.getAttribute("i")==null){
			session.setAttribute("i", 1);
		}else{
			session.setAttribute("i", ((int)session.getAttribute("i"))+1);
			
		}
		return "hi";
	}
	
	@RequestMapping("/{id}")
	public String rest(@PathVariable("id") Long id,String name){
		System.out.println(id);
		if(name!=null){
			System.out.println(name);
		}
		return "hi";
	}
	
	
	public static void main(String[] args) {
		
		User user = new User();
		System.out.println(user.getId());
	}
	
	@RequestMapping("/test")
	public String test(Long id){
		//System.out.println(questions.size());
//		Optionen optionen = new Optionen();
//		optionen.setSigns(1L);
//		optionen.setContent("选项一");
//		optionen.setQuestionId(1L);
//		optionService.addOptionen(optionen);
//		System.out.println(optionen.getId()==null);
		
		if(id!=null){
			if(id==1L){
				
			}else if(id==2L){
			
			}
		}
		
		
		/*Record record = new Record();
		record.setAttendanceId(1L);
		record.setStudentId(1L);
		record.setValidate(true);
		recordService.addRecord(record);*/
		
//		int i = questionMapper.lastInsertId();
//		System.out.println(i);
		
		return "hi";
	}
	

}
